home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.01 Jan 91 / ProjectX / ProjectX Code / projectX.p < prev    next >
Encoding:
Text File  |  1990-03-11  |  14.1 KB  |  572 lines  |  [TEXT/MPS ]

  1. Program ProjectX;
  2.  
  3. USES
  4.     Types,                { Nothing }
  5.     Errors,                { Nothing }
  6.     
  7.     Memory,                { Types }
  8.     OSUtils,            { Types }
  9.     QuickDraw,        { Types }
  10.     Resources,        { Types }
  11.     SegLoad,            { Types }
  12.     
  13.     Controls,            { QuickDraw }
  14.     Events,                { QuickDraw }
  15.     Fonts,                { QuickDraw }
  16.     Menus,                { QuickDraw }
  17.     TextEdit,            { QuickDraw }
  18.     ToolUtils,        { QuickDraw }
  19.     
  20.     OSEvents,            { Types, Events, OSUtils }
  21.     Desk,                    { Types, QuickDraw, Events }
  22.     
  23.     Windows,            { Events, Controls }
  24.     Dialogs,            { Windows, TextEdit }
  25.     Devices,            { OSUtils, Files, QuickDraw }
  26.     Lists,                { Controls }
  27.     Packages,            { Dialogs, Files }
  28.     Script;                { Packages }
  29.  
  30. (*
  31.  
  32. The following lines will rez, compile, link, and execute ProjectX
  33.  
  34. Rez Types.r SysTypes.r Pict.r ProjectX.rsrc -rd -a -o ProjectX
  35.  
  36. Pascal ProjectX.p
  37. Link ProjectX.p.o {PLibraries}PasLib.o {Libraries}Interface.o {Libraries}Runtime.o -o ProjectX
  38. ProjectX
  39.  
  40. *)
  41.  
  42. CONST
  43.     TitleBarHeight    =    19;
  44.     ScrollBarWidth    =    16;
  45.     
  46.     Lastmenu    =    4;
  47.     
  48.     appleMenu            =    1;
  49.         AboutItem            =    1;
  50.     
  51.     fileMenu            =    2;
  52.         QuitItem            =    1;
  53.     
  54.     editMenu            =    3;
  55.     
  56.     windowMenu        =    4;
  57.         ReAdjustItem    =    1;
  58.         
  59.     projXwindowKind        =    2000;
  60.     
  61.     MainWindowType        =    10;
  62.     TitleWindowType        =    11;
  63.     GoAwayWindowType    =    12;
  64.     ZoomBoxWindowType    =    13;
  65.     GrowBoxWindowType    =    14;
  66.     VScrollWindowType    =    15;
  67.     HScrollWindowType    =    16;
  68.     
  69. VAR
  70.     theEvent        :    EventRecord;
  71.     theChar            :    Char;
  72.     tempWindow     :    WindowPtr;
  73.     Doneflag         :    Boolean;
  74.     theScreen        :    Rect;
  75.     
  76.     theItem            :    Integer;
  77.     theMenu            :    Integer;
  78.     
  79.     ItemHit            :    Integer;
  80.     myMenus            :    Array[1..Lastmenu] of MenuHandle;
  81.     itype             :    Integer;
  82.     item                :    Handle;
  83.     box                    :    Rect;
  84.     
  85.     GrowReturn    :    Longint;
  86.     
  87.     WNEExists        :    Boolean;
  88.     
  89.     { * Window vars * }
  90.     theMainWindow            :    WindowPtr;
  91.     theTitleWindow        :    WindowPtr;
  92.     theGoAwayWindow        :    WindowPtr;
  93.     theZoomWindow            :    WindowPtr;
  94.     theGrowWindow            :    WindowPtr;
  95.     theVScrollWindow    :    WindowPtr;
  96.     theHScrollWindow    :    WindowPtr;
  97.     
  98.     MainWindowRect        :    Rect;
  99.     
  100.     GoAwayBoxWidth        :    Integer;
  101.     ZoomBoxWidth            :    Integer;
  102.     
  103.     
  104. function MyGetNextEvent(evtMask:Integer):Boolean; forward;
  105.  
  106. {----------------------------------------------------------------------------------}
  107. Function Str(i:longInt):str255;
  108.  
  109. var
  110.     myStr    :    Str255;
  111.  
  112. begin
  113.     NumToString(i,myStr);
  114.     Str:=myStr;
  115. end;
  116. {----------------------------------------------------------------------------------}
  117. Function Val(s:str255):Longint;
  118.  
  119. var
  120.     myVal    :    Longint;
  121.     
  122. begin
  123.     StringToNum(s,myVal);
  124.     Val:=myVal;
  125. end;
  126. {----------------------------------------------------------------------------------}
  127. function GetScreenBits:Rect;
  128.  
  129. type
  130.     IntPtr    =    ^Integer;
  131.     
  132. var
  133.     WMGRPort    :    GrafPtr;
  134.     
  135. begin
  136.     If IntPtr($28E)^=$3FFF then
  137.         GetScreenBits:=GetGDevice^^.gdRect
  138.     else
  139.         begin
  140.             GetWMGRPort(WMGRPort);
  141.             GetScreenBits:=WMGRPort^.portRect;
  142.         end;
  143. end;
  144. {------------------------------------------------------------------------------}
  145. Procedure SetUpMenus;
  146.  
  147. var
  148.     i    :    Integer;
  149.     
  150. begin
  151.     InitMenus;
  152.     
  153.     { * Get and insert menus * }
  154.     for i:=1 to Lastmenu do
  155.         begin
  156.             MyMenus[i]:=GetMenu(i);
  157.             InsertMenu(myMenus[i],0);
  158.         end;
  159.     AddResMenu(MyMenus[appleMenu],'DRVR');
  160.     
  161.     DrawMenuBar;
  162. end;
  163. {----------------------------------------------------------------------------------}
  164. Procedure DoMyUpdate;
  165.  
  166. var
  167.     TitleOffSet    :    Integer;
  168.     savePort        :    GrafPtr;
  169.     thePICT            :    PicHandle;
  170.     eRect                :    Rect;
  171.     
  172. begin
  173.     tempWindow:=WindowPtr(theEvent.message);
  174.     GetPort(savePort);
  175.     SetPort(tempWindow);
  176.     BeginUpdate(tempWindow);
  177.         with windowPeek(tempWindow)^ do
  178.             If windowKind=projXwindowKind then
  179.                 begin
  180.                     EraseRect(port.portRect);
  181.                     
  182.                     If dataHandle=handle(TitleWindowType) then
  183.                         begin
  184.                             with port.portRect do
  185.                                 begin
  186.                                     MoveTo(left,top+3);
  187.                                     LineTo(right,top+3);
  188.                                     
  189.                                     MoveTo(left,top+5);
  190.                                     LineTo(right,top+5);
  191.                                     
  192.                                     MoveTo(left,top+7);
  193.                                     LineTo(right,top+7);
  194.                                     
  195.                                     MoveTo(left,top+9);
  196.                                     LineTo(right,top+9);
  197.                                     
  198.                                     MoveTo(left,top+11);
  199.                                     LineTo(right,top+11);
  200.                                     
  201.                                     MoveTo(left,top+13);
  202.                                     LineTo(right,top+13);
  203.                                     
  204.                                     TitleOffSet:=Right Div 2-TitleWidth Div 2;
  205.                                     
  206.                                     SetRect(eRect,left+TitleOffSet-3,top,left+TitleOffSet+titleWidth+3,bottom);
  207.                                     EraseRect(eRect);
  208.                                     
  209.                                     MoveTo(left+TitleOffSet,13);
  210.                                     HLock(handle(titleHandle));
  211.                                     DrawString(titleHandle^^);
  212.                                     HunLock(handle(titleHandle));
  213.                                 end;
  214.                         end
  215.                     else if (dataHandle=handle(VScrollWindowType)) OR (dataHandle=handle(HScrollWindowType)) then
  216.                         DrawControls(tempWindow);
  217.                 end;
  218.     EndUpdate(tempWindow);
  219.     SetPort(savePort);
  220. end;
  221. {----------------------------------------------------------------------------------}
  222. Procedure DoAbout;
  223.  
  224. var
  225.     SavePort    :    GrafPtr;
  226.     Dialog        :    DialogPtr;
  227.     
  228. begin
  229.     GetPort(SavePort);
  230.     
  231.     Dialog:=GetNewDialog(1,Nil,Pointer(-1));
  232.     DrawDialog(Dialog);
  233.     
  234.     Repeat Until MyGetNextEvent(mDownMask+keyDownMask+autoKeyMask);
  235.     
  236.     SetPort(SavePort);
  237.     DisposDialog(Dialog);
  238. end;
  239. {----------------------------------------------------------------------------------}
  240. procedure SizeTheWindow;
  241.     
  242. begin
  243.     MainWindowRect:=windowPeek(theMainWindow)^.strucRgn^^.RgnBBox;
  244.     
  245.     with MainWindowRect do
  246.         begin
  247.             MoveWindow(theGoAwayWindow,left+1,top-TitleBarHeight+2,false);
  248.             MoveWindow(theTitleWindow,left+GoAwayBoxWidth+1,top-TitleBarHeight+2,false);
  249.             MoveWindow(theZoomWindow,right+ScrollBarWidth-2-ZoomBoxWidth,top-TitleBarHeight+2,false);
  250.             MoveWindow(theVScrollWindow,right,top+1,false);
  251.             MoveWindow(theGrowWindow,right,bottom,false);
  252.             MoveWindow(theHScrollWindow,left+1,bottom,false);
  253.         end;
  254. end;
  255. {----------------------------------------------------------------------------------}
  256. procedure CreateNewWindow;
  257.  
  258. var
  259.     savePort    :    GrafPtr;
  260.     thePict        :    PicHandle;
  261.     bounds        :    Rect;
  262.     theCTRL        :    ControlHandle;
  263.     
  264. begin
  265.     GetPort(savePort);
  266.     
  267.     { * Set up main window * }
  268.     with theScreen do
  269.         SetRect(bounds,left+40,top+50,right-40,bottom-40);
  270.     theMainWindow:=NewWindow(Nil,bounds,'',true,plainDBox,pointer(-1),false,0);
  271.     with windowPeek(theMainWindow)^ do
  272.         begin
  273.             windowKind:=projXwindowKind;
  274.             dataHandle:=handle(MainWindowType);
  275.         end;
  276.     
  277.     { * Make global copy * }
  278.     MainWindowRect:=windowPeek(theMainWindow)^.strucRgn^^.RgnBBox;
  279.     
  280.     { * Set up go away box * }
  281.     thePict:=PicHandle(GetResource('PICT',1002));
  282.     with thePict^^.picFrame do
  283.         SetRect(bounds,0,0,right-left,bottom-top);
  284.     theGoAwayWindow:=NewWindow(Nil,bounds,'',true,plainDBox,pointer(-1),false,0);
  285.     SetWindowPic(theGoAwayWindow,thePict);
  286.     with windowPeek(theGoAwayWindow)^ do
  287.         begin
  288.             windowKind:=projXwindowKind;
  289.             dataHandle:=handle(GoAwayWindowType);
  290.         end;
  291.     with theGoAwayWindow^.portRect do
  292.         GoAwayBoxWidth:=(right-left);
  293.     
  294.     { * Set up zoom box * }
  295.     thePict:=PicHandle(GetResource('PICT',1000));
  296.     with thePict^^.picFrame do
  297.         SetRect(bounds,0,0,right-left,bottom-top);
  298.     theZoomWindow:=NewWindow(Nil,bounds,'',true,plainDBox,pointer(-1),false,0);
  299.     SetWindowPic(theZoomWindow,thePict);
  300.     with windowPeek(theZoomWindow)^ do
  301.         begin
  302.             windowKind:=projXwindowKind;
  303.             dataHandle:=handle(ZoomBoxWindowType);
  304.         end;
  305.     with theZoomWindow^.portRect do
  306.         ZoomBoxWidth:=(right-left);
  307.     
  308.     { * Set up the title window * }
  309.     with MainWindowRect do
  310.         SetRect(bounds,left+GoAwayBoxWidth+1,top-TitleBarHeight+1,right-ZoomBoxWidth+ScrollBarWidth-1,top-1);
  311.     theTitleWindow:=NewWindow(Nil,bounds,'Untitled',true,plainDBox,pointer(-1),false,0);
  312.     with windowPeek(theTitleWindow)^ do
  313.         begin
  314.             windowKind:=projXwindowKind;
  315.             dataHandle:=handle(TitleWindowType);
  316.         end;
  317.     SetPort(theTitleWindow);
  318.     TextFont(SystemFont);
  319.     
  320.     { * Set up grow box * }
  321.     thePict:=PicHandle(GetResource('PICT',1001));
  322.     with thePict^^.picFrame do
  323.         SetRect(bounds,0,0,right-left,bottom-top);
  324.     theGrowWindow:=NewWindow(Nil,bounds,'',true,plainDBox,pointer(-1),false,0);
  325.     SetWindowPic(theGrowWindow,thePict);
  326.     with windowPeek(theGrowWindow)^ do
  327.         begin
  328.             windowKind:=projXwindowKind;
  329.             dataHandle:=handle(GrowBoxWindowType);
  330.         end;
  331.     
  332.     { * Set up the vertical scrollbar window * }
  333.     with MainWindowRect do
  334.         SetRect(bounds,right,top+1,right+ScrollBarWidth-2,bottom-1);
  335.     theVScrollWindow:=NewWindow(Nil,bounds,'',true,plainDBox,pointer(-1),false,0);
  336.     with windowPeek(theVScrollWindow)^ do
  337.         begin
  338.             windowKind:=projXwindowKind;
  339.             dataHandle:=handle(VScrollWindowType);
  340.         end;
  341.     SetPort(theVScrollWindow);
  342.     ValidRect(theVScrollWindow^.portRect);
  343.     
  344.     bounds:=theVScrollWindow^.portRect;
  345.     InsetRect(bounds,-1,-1);
  346.     theCTRL:=NewControl(theVScrollWindow,bounds,'',true,1,1,255,scrollBarProc,0);
  347.     
  348.     { * Set up the horizontal scrollbar window * }
  349.     with MainWindowRect do
  350.         SetRect(bounds,left+1,bottom,right-1,bottom+ScrollBarWidth-2);
  351.     theHScrollWindow:=NewWindow(Nil,bounds,'',true,plainDBox,pointer(-1),false,0);
  352.     with windowPeek(theHScrollWindow)^ do
  353.         begin
  354.             windowKind:=projXwindowKind;
  355.             dataHandle:=handle(HScrollWindowType);
  356.         end;
  357.     SetPort(theHScrollWindow);
  358.     ValidRect(theHScrollWindow^.portRect);
  359.     
  360.     bounds:=theHScrollWindow^.portRect;
  361.     InsetRect(bounds,-1,-1);
  362.     theCTRL:=NewControl(theHScrollWindow,bounds,'',true,1,1,255,scrollBarProc,0);
  363.     
  364.     { * Hilite the title window * }
  365.     HiliteWindow(theTitleWindow,true);
  366.     
  367.     SizeTheWindow;
  368.     
  369.     SetPort(savePort);
  370. end;
  371. {----------------------------------------------------------------------------------}
  372. Procedure DoCommand(mResult:longint);
  373.  
  374. var
  375.     DArefNum    :    Integer;
  376.     DAname        :    Str255;
  377.     
  378. begin
  379.     theMenu:=HiWrd(mResult);
  380.     theItem:=LoWrd(mResult);
  381.     
  382.     case theMenu of
  383.         appleMenu:
  384.             If theItem=AboutItem then
  385.                 DoAbout
  386.             else
  387.                 begin
  388.                     GetItem(myMenus[appleMenu],theItem,DAname);
  389.                     DArefNum:=OpenDeskAcc(DAname);
  390.                 end;
  391.         fileMenu:
  392.             If theItem=QuitItem then
  393.                 DoneFlag:=true;
  394.         windowMenu:
  395.             Case theItem of
  396.                 ReAdjustItem:
  397.                     SizeTheWindow;
  398.             end;
  399.     end;{case}
  400.     
  401.     HiliteMenu(0);
  402. end;
  403. {----------------------------------------------------------------------------------}
  404. procedure DragMyWindow(startingPt:Point);
  405.  
  406. var
  407.     AllWindowsRgn    :    RgnHandle;
  408.     moveMent            :    Longint;
  409.     dx,dy                    :    Integer;
  410.     
  411. begin
  412.     AllWindowsRgn:=NewRgn;
  413.     
  414.     OpenRgn;
  415.     
  416.     FrameRgn(windowPeek(theMainWindow)^.strucRGN);
  417.     FrameRgn(windowPeek(theTitleWindow)^.strucRGN);
  418.     FrameRgn(windowPeek(theGoAwayWindow)^.strucRGN);
  419.     FrameRgn(windowPeek(theZoomWindow)^.strucRGN);
  420.     FrameRgn(windowPeek(theGrowWindow)^.strucRGN);
  421.     FrameRgn(windowPeek(theVScrollWindow)^.strucRGN);
  422.     FrameRgn(windowPeek(theHScrollWindow)^.strucRGN);
  423.     
  424.     CloseRgn(AllWindowsRgn);
  425.     
  426.     moveMent:=DragGrayRgn(AllWindowsRgn,startingPt,theScreen,theScreen,noConstraint,NIL);
  427.     
  428.     If Point(moveMent).h=$8000 then
  429.         dx:=0
  430.     else
  431.         dx:=Point(moveMent).h;
  432.     
  433.     If Point(moveMent).v=$8000 then
  434.         dy:=0
  435.     else
  436.         dy:=Point(moveMent).v;
  437.     
  438.     If (dx<>0) OR (dy<>0) then
  439.         begin
  440.             with windowPeek(theMainWindow)^.strucRGN^^.RgnBBox do
  441.                 MoveWindow(theMainWindow,left+dx,top+dy,false);
  442.             
  443.             with windowPeek(theGoAwayWindow)^.strucRGN^^.RgnBBox do
  444.                 MoveWindow(theGoAwayWindow,left+dx,top+dy,false);
  445.                 
  446.             with windowPeek(theTitleWindow)^.strucRGN^^.RgnBBox do
  447.                 MoveWindow(theTitleWindow,left+dx,top+dy,false);
  448.             
  449.             with windowPeek(theZoomWindow)^.strucRGN^^.RgnBBox do
  450.                 MoveWindow(theZoomWindow,left+dx,top+dy,false);
  451.                 
  452.             with windowPeek(theVScrollWindow)^.strucRGN^^.RgnBBox do
  453.                 MoveWindow(theVScrollWindow,left+dx,top+dy,false);
  454.             
  455.             with windowPeek(theGrowWindow)^.strucRGN^^.RgnBBox do
  456.                 MoveWindow(theGrowWindow,left+dx,top+dy,false);
  457.                 
  458.             with windowPeek(theHScrollWindow)^.strucRGN^^.RgnBBox do
  459.                 MoveWindow(theHScrollWindow,left+dx,top+dy,false);
  460.         end;
  461.     
  462.     DisposeRgn(AllWindowsRgn);
  463. end;
  464. {----------------------------------------------------------------------------------}
  465. function CurrentWindowHit(theWindow:WindowPtr):Boolean;
  466.  
  467. begin
  468.     CurrentWindowHit:=((theWindow<>theMainWindow) | (theWindow<>theTitleWindow)
  469.                                             | (theWindow<>theGoAwayWindow) | (theWindow<>theZoomWindow)
  470.                                             | (theWindow<>theGrowWindow) | (theWindow<>theVScrollWindow)
  471.                                             | (theWindow<>theHScrollWindow));
  472. end;
  473. {----------------------------------------------------------------------------------}
  474. Procedure InitGlob;
  475.  
  476. const
  477.     UnImplTrapNum                        =    $9F;        { * Unimplemented trap * }
  478.     WaitNextEventTrapWord        =    $60;
  479.  
  480. var
  481.     theWorld    :    SysEnvRec;
  482.     
  483. begin
  484.     { * Get the world, so to speak * }
  485.     If SysEnvirons(1,theWorld)<>envNotPresent then
  486.         WNEExists:=(theWorld.machineType>=0) & (NGetTrapAddress(WaitNextEventTrapWord,ToolTrap)<>NGetTrapAddress(UnImplTrapNum,ToolTrap))
  487.     else
  488.         WNEExists:=false;
  489.         
  490.     theScreen:=GetScreenBits;
  491.     
  492.     CreateNewWindow;
  493. end;
  494. {----------------------------------------------------------------------------------}
  495. function MyGetNextEvent(evtMask:Integer):Boolean;
  496.  
  497. begin
  498.     If WNEExists then
  499.         MyGetNextEvent:=WaitNextEvent(evtMask,theEvent,6,Nil)
  500.     else
  501.         begin
  502.             SystemTask;
  503.             MyGetNextEvent:=GetNextEvent(evtMask,theEvent);
  504.         end;
  505. end;
  506. {----------------------------------------------------------------------------------}
  507. procedure _DataInit;EXTERNAL;
  508.  
  509. begin
  510.     { * Get rid of MPW's init code * }
  511.     UnloadSeg(@_DataInit);
  512.     
  513.     FlushEvents(EveryEvent,0);
  514.     InitGraf(@thePort);
  515.     InitFonts;
  516.     TEInit;
  517.     InitWindows;
  518.     InitDialogs(Nil);
  519.     SetUpMenus;
  520.     InitGlob;
  521.     
  522.     Repeat
  523.         InitCursor;
  524.         If MyGetNextEvent(everyEvent) then
  525.             with theEvent do
  526.                 case what of
  527.                     mouseDown:
  528.                         case FindWindow(where,tempWindow) of
  529.                             inMenuBar:
  530.                                 DoCommand(MenuSelect(where));
  531.                             InSysWindow:
  532.                                 SystemClick(theEvent,tempWindow);
  533.                             inContent:
  534.                                 If NOT CurrentWindowHit(tempWindow) then
  535.                                     SelectWindow(tempWindow)
  536.                                 else if BAnd(modifiers,cmdKey)<>0 then
  537.                                     DragWindow(tempWindow,where,GetGrayRGN^^.RGNBBox)
  538.                                 else if tempWindow=theTitleWindow then
  539.                                     DragMyWindow(where)
  540.                                 else if tempWindow=theGrowWindow then
  541.                                     begin
  542.                                         GrowReturn:=GrowWindow(theMainWindow,where,GetGrayRGN^^.RGNBBox);
  543.                                         SizeWindow(theMainWindow,LoWrd(GrowReturn),HiWrd(GrowReturn),true);
  544.                                     end
  545.                                 else if tempWindow=theGoAwayWindow then
  546.                                     begin
  547.                                         
  548.                                     end
  549.                                 else if tempWindow=theZoomWindow then
  550.                                     begin
  551.                                         
  552.                                     end;
  553.                             inDrag:
  554.                                 If NOT CurrentWindowHit(tempWindow) then
  555.                                     SelectWindow(tempWindow)
  556.                                 else If BAnd(modifiers,cmdKey)<>0 then
  557.                                     DragWindow(tempWindow,where,GetGrayRGN^^.RGNBBox);
  558.                         end;{case}
  559.                     keyDown,autoKey:
  560.                         begin
  561.                             theChar:=chr(BitAnd(message,255));
  562.                             If BitAnd(modifiers,CmdKey)<>0 then
  563.                                 DoCommand(MenuKey(theChar))
  564.                         end;
  565.                     updateEvt:
  566.                         DoMyUpdate;
  567.                 end;
  568.     Until Doneflag;
  569.     
  570.     SetCursor(GetCursor(WatchCursor)^^);
  571. end.
  572.